/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package map;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.jdesktop.swingx.painter.Painter;

/**
 *
 * @author Pascal
 */
public class TrackPainter implements Painter<JXMapViewer> {

    List<GeoPosition> positions;
    
    public TrackPainter(GeoPosition... position) {
        positions = new ArrayList<>();

        if (position != null) {
            positions.addAll(Arrays.asList(position));
        }
    }
    
    public TrackPainter() {
        this(null);
    }
    
    public void addPosition(GeoPosition pos) {
        if (pos == null) {
            throw new NullPointerException();
        }
        
        positions.add(pos);
    }
    
    public void clear() {
        positions.clear();
    }
    
    @Override
    public void paint(Graphics2D g, JXMapViewer map, int w, int h) {
                g = (Graphics2D) g.create();

		Rectangle rect = map.getViewportBounds();
		g.translate(-rect.x, -rect.y);

		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		g.setColor(Color.RED);
		g.setStroke(new BasicStroke(2));

		int lastX = 0;
		int lastY = 0;

                boolean first = true;
                
		for (GeoPosition gp : positions)
		{
			Point2D pt = map.getTileFactory().geoToPixel(gp, map.getZoom());

                        if (first) {
                            first = false;
                        }
                        else {
                            g.drawLine(lastX, lastY, (int) pt.getX(), (int) pt.getY());
                        }

			lastX = (int) pt.getX();
			lastY = (int) pt.getY();
		}

		g.dispose();
    }
    
}
